package service

import (
	"errors"
	"go-gin-api/app/model"
	"go-gin-api/app/model/dto"
	"go-gin-api/exception"
	"go-gin-api/util"
)

type User struct {
	Service
}

func (u *User) AddUser(dto *dto.AddUserDTO) (err error) {
	// 到这里，验证结束
	user := new(model.User)
	err = user.CheckUser(u.Orm, dto.Name)
	if user.ID == 0 {
		user.LoginName = dto.Name
		user.Password = dto.Password
		err = user.CreateUser(u.Orm)
	} else {
		//err = errors.New("用户名已存在")
		err = exception.UsernameUsed
	}
	return
}

func (u *User) CheckLogin(dto *dto.GetUserDTO) (user model.User, err error) {
	user.CheckLogin(u.Orm, dto.Name)
	if user.Password != util.ScryptPassword(dto.Password) {
		err = errors.New("密码错误")
	}
	return
}
